import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { queryUser } from '@/services/user';

export interface UserModelState {
    data: any;
}

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: UserModelState) => T) => T },
) => void;

export interface ProductModelType {
    namespace: string;
    state: UserModelState;
    effects: {
        fetch: Effect;
    };
    reducers: {
        save: Reducer<UserModelState>;
    };
}

const Model: ProductModelType = {
    namespace: 'user',

    state: {
        data: {
            rows: [],
        },
    },

    effects: {
        *fetch({ payload }, { call, put }) {
            const response = yield call(queryUser, payload);
            yield put({
                type: 'save',
                payload: response,
            });
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
    },
};

export default Model;